package com.filepublic.demo.server.config;

import com.filepublic.demo.common.property.ImageProperty;
import com.filepublic.demo.common.property.TextProperty;
import com.filepublic.demo.server.intercapter.MyIntercapter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

@Configuration
public class MvcConfigation extends WebMvcConfigurationSupport {

    @Resource
    MyIntercapter myIntercapter;

    @Resource
    ImageProperty imageProperty;

    @Resource
    TextProperty textProperty;

    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .exposedHeaders("*")
                .allowedMethods("POST","GET","PUT","DELETE","OPTIONS")
                .maxAge(500);
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myIntercapter)
                .addPathPatterns("/penpal/**")
                .addPathPatterns("/email/**")
                .addPathPatterns("/file/**");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //文本路径
        registry.addResourceHandler(textProperty.getTextUrl())
                .addResourceLocations("file:"+textProperty.getTextOriginPath());

        //图片路径
        registry.addResourceHandler(imageProperty.getImageUrl())
                .addResourceLocations("file:"+imageProperty.getImageOriginPath());

        //swagger数据源
        registry.addResourceHandler("/doc.html").
                addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").
                addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
